package com.apes.fn.scm.retailOrder.service;

import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.retailOrder.repository.RetailOrderRepository;
import com.apes.fn.scm.retailOrder.service.account.RetailRetailAccountService;
import com.apes.fn.scm.retailReturn.RetailReturn;
import com.apes.fn.scm.retailReturn.RetailReturnRepository;
import com.apes.fn.server.servicePlan.model.ServicePlan;
import com.apes.fn.server.servicePlan.model.ServicePlanItem;
import com.apes.fn.server.servicePlan.repository.ServicePlanRepository;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.voucher.model.Voucher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service("pickFlowRetailReturnVoucherService")
public class PickFlowRetailReturnVoucherService {

    @Autowired
    RetailReturnRepository retailReturnRepository;

    @Autowired
    private RetailRetailAccountService retailRetailAccountService;

    @Autowired
    private RetailService retailService;

    @Autowired
    private RetailOrderRepository retailOrderRepository;


    public Voucher test(SimpleRequest request) {
        String id = request.get("id");
        RetailReturn retailReturn = retailReturnRepository.findOne(id);
        return process(retailReturn);
    }

    public Voucher flowRetailReturnVoucher(SimpleRequest simpleRequest) {
        RetailReturn retailReturn = simpleRequest.getO(RetailReturn.class);
        if (retailReturn == null) throw new RuntimeException("退货订单不存在！");
        retailService.sendRetailVouched(retailOrderRepository.findRetailOrder(retailReturn.getId())); //建行生活零售退货生成客户返利单
        Voucher voucher = process(retailReturn);
        return voucher;
    }


    private Voucher process(RetailReturn retailReturn) {
        /** 开始区分销售退回凭证类型 **/

        if ("A".equals(retailReturn.getOrderType().getCode())) {
            checkData(retailReturn);

            // 异业合作退货需要单独处理
            if ("RO004".equals(retailReturn.getSaleOrder().getScene().getId())) {

                //  区分是否为单位客户
                ServicePlan servicePlan = SpringManager.getBean(ServicePlanRepository.class).findOne( retailReturn.getSaleOrder().getSource());
                List<ServicePlanItem> unitClientPlanItems = servicePlan.getServicePlanItems().stream()
                        .filter(servicePlanItem -> Objects.nonNull(servicePlanItem.getUnitClient()))
                        .collect(Collectors.toList());
                if (unitClientPlanItems.size() == 0) {
                    return retailRetailAccountService.process("returnCrossIC", retailReturn);   // 异业退回
                } else {
                    return retailRetailAccountService.process("returnUnitClient", retailReturn);    // 单位客户退回
                }
            }

            // 有原单退回
            return retailRetailAccountService.process("returnWithOrder", retailReturn);     // 委托定损走这里
        } else {
            // 无原单退回
            return retailRetailAccountService.process("returnWithoutOrder", retailReturn);
        }
    }

    private void checkData(RetailReturn retailReturn) {
        // 有原单数据检查
        if ("A".equals(retailReturn.getOrderType().getCode())) {
            RetailOrder saleOrder = retailReturn.getSaleOrder();
            if (saleOrder == null) throw new RuntimeException("有原单退货业务，原单不允许为空！");

            retailReturn.getSaleItems().forEach(
                    retailReturnItem -> {
                        if (retailReturnItem.getRetailOrderItem() == null) {
                            throw new RuntimeException("有原单退货业务，原单明细不允许为空！零售退货明细行为：" + retailReturnItem.getId());
                        }
                    });
        }
    }
}
